package com.mod.reanimator 
{
	import com.calefk.core.EntitySimple;
	import com.calefk.core.Project;
	import com.calefk.modules.path.Node;
	import com.calefk.modules.path.PathFinding;
	import com.calefk.util.helper;
	import com.calefk.util.Iterator;
	import com.mod.enemies.EnemyManager;
	import com.mod.enemies.Movement;
	import com.mod.Global;
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author authorX
	 */
	public class villager extends BaseVillager 
	{
		
		private var mPathFollow:Array;
		private var mIterator:Iterator;
		private var mMovement:Movement;
		private var mEnemyManager:EnemyManager;
		
		private var mAlpha:int = 1;
		
		public function villager(canvas:MovieClip, nameClip:String=null, px:Number=0, py:Number=0, project:Project=null,enemyManager:EnemyManager=null) 
		{
			super(canvas, nameClip, px, py, project);
			mClip.endReviving = endReviving;
			mClip.rotation = helper.randomInterval( -180, 180);
			currentState = BaseVillager.DEATH;
			collision = mClip.collision_mc;
			
			myBar = new BarVillager(canvas, "bar", 50, 20,project,this);
			myBar.clip.visible = false;
			
			mEnemyManager = enemyManager;
			
			mTotalLife = 10;
			mLife = mTotalLife;
		}
		
		override public function switchState(newState:int):void 
		{
			super.switchState(newState);
						
			switch(currentState)
			{
				case BaseVillager.DEATH:
					mClip.gotoAndStop("dying");
					break;
				case BaseVillager.EXPLODING:
					mClip.gotoAndStop("exploding");
					break;
				case BaseVillager.MOVING:
					mClip.gotoAndStop("moving");
					break;
				case BaseVillager.REVIVING:
					mClip.gotoAndStop("reviving");
					break;
			}
			
		}

		
		private function endReviving():void 
		{
			switchState(BaseVillager.MOVING);
			mClip.rotation = 0;
			if (healthBar) healthBar.visible = true;
			
		/* Obtener Pathfinding */
			var row   :int = y / Global.TILE_WIDTH;
			var column:int = x / Global.TILE_HEIGHT;
			mPathFollow = PathFinding.getPath(Global.mapBits , row , column , 9 , 9 , 0);
						
			if (mPathFollow != null && mPathFollow.length > 1) {
				mPathFollow.splice(0 , 1);
				mIterator = new Iterator(mPathFollow);
				
				if (mIterator) {
					if (mIterator.hasNext()) {
						var node:Node = mIterator.next();
						var endX:Number = (node.j * Global.TILE_WIDTH) + (Global.TILE_WIDTH * 0.5);
						var endY:Number = (node.i * Global.TILE_HEIGHT) + (Global.TILE_HEIGHT * 0.5)
						mMovement = new Movement(this , mClip.x , mClip.y , endX , endY , 0.5);
						mMovement.onEndMovement = onEndMovement;
					}
				}	
			}			
			
		}
		
		private function onEndMovement():void {
			if (mIterator) {
				if (mIterator.hasNext()) {
					var node:Node = mIterator.next();
					var endX:Number = (node.j * Global.TILE_WIDTH ) + (Global.TILE_WIDTH * 0.5);
					var endY:Number = (node.i * Global.TILE_HEIGHT) + (Global.TILE_HEIGHT * 0.5)
					mMovement = new Movement(this , mClip.x , mClip.y , endX , endY, 0.5);
					mMovement.onEndMovement = onEndMovement;
				} else {
					mMovement = null;
					onEnterBase();
				}
			}			
		}		
		
		private function onEnterBase():void {
			mClip.alpha = 1;
			Global.playSound("snd_in_base", 1, 5);
			switchState(ENTER_BASE);
			// se agregará ula entrada base al heart skin
			Global.engine.heartSkin.addCounterEnterBase(1);
		}
				
		
		public override function update(dt:int):void
		{
			switch(currentState)
			{
				case BaseVillager.DEATH:
					mClip.gotoAndStop("dying");
					break;
				case BaseVillager.EXPLODING:
					mClip.gotoAndStop("exploding");
					break;
				case BaseVillager.MOVING:
					mClip.gotoAndStop("moving");
					break;
				case BaseVillager.REVIVING:
					mClip.gotoAndStop("reviving");
					break;
				case BaseVillager.FIGHTING:
					mClip.gotoAndStop("attacking");
					break;
			}
			
			if (mMovement && currentState!=BaseVillager.EXPLODING) {
				mMovement.update(dt);
			}
			
			switch (currentState) {
				case BaseVillager.ENTER_BASE:
					mClip.alpha -= 0.05;
					if (mClip.alpha < 0) {
						mClip.alpha = 0;
						toClean = true;
					}
					break;
			}

		}
		public function movingToBase():void
		{
			
		}
		
		
		
	}

}